<template>
  <n-drawer
    v-model:show="isDrawer"
    :width="width"
    placement="right"
    :auto-focus="false"
    @after-leave="handleReset"
  >
    <n-drawer-content :title="title" closable>
      <n-form
        :model="formParams"
        :rules="rules"
        ref="formRef"
        label-placement="left"
        :label-width="80"
      >
        <n-form-item label="部门名称" path="dept_name">
          <n-input placeholder="请输入部门名称" v-model:value="formParams.dept_name" />
        </n-form-item>
        <n-form-item label="上级部门" path="parent">
          <n-tree-select
            label-field="dept_name"
            value-field="dept_id"
            key-field="dept_id"
            :options="getTreeList"
            clearable
            v-model:value="formParams.parent"
            placeholder="请选择上级部门"
          />
        </n-form-item>
        <n-form-item label="排序" path="order_num">
          <n-input-number placeholder="请输入排序" v-model:value="formParams.order_num" />
        </n-form-item>
        <n-form-item label="状态" path="is_enable">
          <n-switch v-model:value="formParams.is_enable">
            <template #checked> 启用 </template>
            <template #unchecked> 禁用 </template>
          </n-switch>
        </n-form-item>
      </n-form>

      <template #footer>
        <n-space>
          <n-button @click="handleReset">重置</n-button>
          <n-button type="primary" :loading="subLoading" @click="formSubmit">提交</n-button>
        </n-space>
      </template>
    </n-drawer-content>
  </n-drawer>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { useMessage } from 'naive-ui';
  import type { formParamsType } from './types';

  import { addDept, editDept, deptInfo } from '@/api/auth/dept';

  const rules = {
    code: {
      required: true,
      message: '部门编码不能为空',
      trigger: 'blur',
    },
    name: {
      required: true,
      message: '部门名称不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  const props = defineProps({
    title: {
      type: String,
      default: '添加部门',
    },
    width: {
      type: Number,
      default: 450,
    },
    deptList: {
      type: Array,
    },
  });

  const defaultValueRef = () => ({
    dept_id: null,
    parent: null,
    is_enable: true,
    dept_name: '',
    order_num: null,
  });

  const message = useMessage();
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const getTreeList = computed(() => {
    return props.deptList;
  });

  const formParams = ref<formParamsType>(defaultValueRef());

  function openDrawer(dept_id?) {
    if (dept_id) {
      formParams.value.dept_id = dept_id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((errors) => {
      if (!errors) {
        const msg = formParams.value.dept_id ? '编辑成功' : '添加成功';
        if (formParams.value.dept_id) {
          editDept(formParams.value).then((_) => {
            message.success(msg);
            emit('change');
            handleReset();
            closeDrawer();
          });
        } else {
          addDept(formParams.value).then((_) => {
            message.success(msg);
            emit('change');
            handleReset();
            closeDrawer();
          });
        }
      } else {
        message.error('请填写完整信息');
      }
    });
  }

  function handleReset() {
    formRef.value.restoreValidation();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    deptInfo({ dept_id: formParams.value.dept_id }).then((res) => {
      const params = {
        dept_id: res.dept_id,
        parent: res.parent,
        is_enable: res.is_enable,
        dept_name: res.dept_name,
        order_num: res.order_num,
      };
      formParams.value = Object.assign(formParams.value, params);
      isDrawer.value = true;
    });
  }

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
